package com.seselin.citypicker;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.seselin.citypicker.bean.Area;
import com.seselin.citypicker.bean.City;
import com.seselin.citypicker.bean.Country;
import com.seselin.citypicker.bean.Province;
import com.seselin.citypicker.db.CityDataBase;
import com.seselin.citypicker.view.AreaPicker;
import com.seselin.citypicker.view.CityPicker;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * Created by Seselin on 2016/10/19 15:30.
 */

public class TestCityActivity extends Activity {

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);
        context = this;
    }

    /**
     * 把数据从数据库取出来，保存成json格式
     *
     * @param view
     */
    public void saveCountry(View view) {
        Runnable runnable = new Runnable() {
            public void run() {
                saveCountry();
            }
        };

        new Thread(runnable).start();
    }

    /**
     * 城市选择
     *
     * @param view
     */
    public void selectCity(View view) {
        CityPicker picker = new CityPicker(this);
        picker.setOnPickerListener(new CityPicker.OnPickerListener() {
            @Override
            public void onConfirmClick(String... citySelected) {
                Toast.makeText(TestCityActivity.this, citySelected[0] + "" + citySelected[2] +
                        "\n" +
                        citySelected[1] + "" + citySelected[3], Toast.LENGTH_LONG).show();
            }
        });
        picker.show();
    }

    /**
     * 区域选择
     *
     * @param view
     */
    public void selectArea(View view) {
        AreaPicker picker = new AreaPicker(this);
        picker.setOnPickerListener(new AreaPicker.OnPickerListener() {
            @Override
            public void onConfirmClick(String... citySelected) {
                Toast.makeText(TestCityActivity.this,
                        citySelected[0] + citySelected[1] + citySelected[2]
                        , Toast.LENGTH_LONG).show();
            }
        });
        picker.show();
    }


    public void saveCountry() {
        List<Province> provinces = CityDataBase.getInstance(this).getProvinceList();
        for (int i = 0; i < provinces.size(); i++) {
            String pid = provinces.get(i).getNid();
            List<City> cities = CityDataBase.getInstance(this).getCityList(pid);
            for (int j = 0; j < cities.size(); j++) {
                String cid = cities.get(j).getNid();
                List<Area> areas = CityDataBase.getInstance(this).getAreaList(cid);
                cities.get(j).setAreaList(areas);
            }
            provinces.get(i).setCityList(cities);
        }
        Country country = new Country();
        country.setProvinces(provinces);
        saveObject(this, country);
    }

    public Country getCountry() {
        List<Province> provinces = CityDataBase.getInstance(this).getProvinceList();
        for (int i = 0; i < provinces.size(); i++) {
            String pid = provinces.get(i).getId();
            List<City> cities = CityDataBase.getInstance(this).getCityList(pid);
            for (int j = 0; j < cities.size(); j++) {
                String cid = cities.get(j).getId();
                List<Area> areas = CityDataBase.getInstance(this).getAreaList(cid);
                cities.get(j).setAreaList(areas);
            }
            provinces.get(i).setCityList(cities);
        }
        Country country = new Country();
        country.setProvinces(provinces);
        return country;
    }

    public static void saveObject(Context context, Object object) {
        Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation()
                .create();
        String beanStr = gson.toJson(object);
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        writeFileSdcard(path + "/city.json", beanStr);
    }

    public static void writeFileSdcard(String fileName, String message) {
        File file = new File(fileName);
        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(fileName);
            byte[] bytes = message.getBytes();
            fos.write(bytes);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
